﻿@{
    ViewBag.Title = "接收样品";
}

<div class="row col-md-12 ml-0" style="min-height:95vh;">
    <div class="row col-md-12" style="padding-right:0;min-height:95vh;">
        <div class="row col-md-12" style=" min-height:5vh;">
            <strong class="font-rfs" style="margin-right: 2rem;padding-top: 0.4rem;">接收样品</strong>
            <button id="Runtime-ReceiveSamples-Receive" class="btn btn-rfs"><i class="bi-check-lg"></i> 接收</button>
            <button id="Runtime-ReceiveSamples-upcScan" class="btn btn-rfs"><i class="bi-upc-scan"></i> 扫码</button>
        </div>
        <div class="table-responsive-sm shadow p-0 bg-white rounded col-md-12 pre-scrollable" style="min-height:90vh;">
            <table id="Runtime-ReceiveSamples-Table" class="table table-striped table-bordered table-sm table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th></th>
                        <th>名称</th>
                        <th>样品描述</th>
                        <th>样品ID</th>
                        <th>样品编号</th>
                        <th>批次号</th>
                        <th>类型</th>
                        <th>分类</th>
                        <th>实验室</th>
                        <th>工作组</th>
                        <th>登录时间</th>
                        <th>指派人</th>
                        <th>指派组</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>
</div>
<div class="modal" id="Runtime-ReceiveSamples-Modal" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p id="Runtime-ReceiveSamples-Modal-Title" class="pt-3 pl-3 font-rfs"></p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
            </div>
            <div class="modal-body">
                <div class="input-group">
                    <div class="col-12 pr-0 pl-0">
                        <input id="Runtime-ReceiveSamples-Name" class="btn btn-rfs col-12" style="border: 1px solid rgba(0,0,0,.15);text-align:left;" placeholder="扫码输入" onchange="InputTable(this)" />
                    </div>
                </div>
                <div class="table-responsive-sm modal-table-shadow p-0 bg-white rounded col-md-12 pre-scrollable" style="height: 60vh;">
                    <table id="Runtime-ReceiveSamples-upcTable" class="table table-striped table-bordered-modal table-sm table-hover">
                        <thead>
                            <tr>
                                <th>样品编号</th>
                            </tr>
                        </thead>
                        <tbody style="font-size: calc(0.6em + 0.1vw) !important"></tbody>
                    </table>
                </div>
                <div class="text-right" style="padding-top: 5px;">
                    <button id="Runtime-ReceiveSamples-confirm" type="button" class="btn btn-primary font-rfs-modal" disabled="disabled">确定</button>
                    <button id="Runtime-ReceiveSamples-cancel" type="button" class="btn btn-info font-rfs-modal" data-dismiss="modal">取消</button>
                </div>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script>

        $(document).ready(function () {
            LoadSampling("");
        });

        function setLoginTime(value, row, index) {
            return row.loginTime.replace('T', ' ');
        }

        function LoadSampling(Ids) {
            $('#Runtime-ReceiveSamples-Table').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
            var columns = [{ field: "id", visible: false }, { field: "selectItem", width: "5%", align: "center", checkbox: true }, { field: "name" },
            { field: "description" }, { field: "sampleId" }, { field: "sampleNumber" }, { field: "batchId" },
            { field: "classification" }, { field: "genricClass" }, { field: "site" }, { field: "group" }, { field: "loginTime", formatter: setLoginTime },
            { field: "assignPersonName" }, { field: "assignGroupName" }];

            $('#Runtime-ReceiveSamples-Table').bootstrapTable({
                method: 'post',
                url: "GetReceiveSamples",
                dataType: "json",  // 服务器端返回值类型
                contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
                queryParams: function (params) {
                    var data = {
                        limit: params.limit, // 每页显示多少条记录
                        offset: params.offset, // 偏移量
                        Ids: Ids
                    };
                    return data;
                },
                queryParamsType: "limit", // 服务器端分页，这个参数必需是“limit”
                dataField: "rows",

                uniqueId: "id",          // 设置主键
                sidePagination: "server", // 服务器端分页，这个参数必需设置
                pagination: true,  // 显示分页条,设为true，启用了分页功能
                pageNumber: 1,
                pageSize: 15,   // 默认页大小
                pageList: [5, 10, 15, 20, 50, 100],  // 控制每页数目
                locale: "zh-CN",
                columns: columns,          // 加载列定义, 注意！这个地方绑定的列名称开头大写字母会全部变成小写。

                selectItemName: "selectItem", // 指定选择行ID
                clickToSelect: true, // 相应单击，自动选中此行
                checkboxHeader: true, // 标题行显示全选按钮

                onClickRow: function (row, $element, field) {
                    //$("#Runtime-ReceiveSamples-Table").find("tbody").find("tr").each(function () {
                    //    $(this).removeClass('changeblueColor');
                    //});
                    //$element.addClass("changeblueColor");
                },
                silent: true,  //刷新事件必须设置
                formatLoadingMessage: function () {
                    return "请稍等，正在加载中...";
                },
                formatNoMatches: function () {  //没有匹配的结果
                    return '无符合条件的记录';
                },
                onLoadError: function (data) {
                    $('#Runtime-ReceiveSamples-Table').bootstrapTable('removeAll');
                },
                onLoadSuccess: function (data) {   // 数据加载成功后触发该事件
                    $('.fixed-table-toolbar').css("height", "0px"); /* 隐藏表格上的工具栏 */
                    $('th label').css("margin-bottom", "0px"); // 设置标题行选择控件的样式
                    $('td label').css("margin-bottom", "0px"); // 设置数据行选择控件的样式
                },
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
                },
                formatRecordsPerPage: function (pageNumber) {
                    return " 每页显示" + pageNumber + '条记录';
                }
            });
        }

        $("#Runtime-ReceiveSamples-Receive").click(function () {
            var ids = GetSelectItems();
            if (ids.length == 0) {
                return false;
            }

            $.post("ReceiveSamples", {
                Ids: ids
            }, function (data) {
                alert(data);
            });
        });

        function GetSelectItems() {
            var rows = $('#Runtime-ReceiveSamples-Table').bootstrapTable('getSelections');
            if (rows.length == 0) {
                return;
            } else {
                var arrays = new Array();
                $(rows).each(function () {
                    arrays.push(this.id);
                })

                var ids = arrays.join(',');
                return ids;
            }
        }

        $("#Runtime-ReceiveSamples-upcScan").click(function () {
            $("#Runtime-ReceiveSamples-Modal-Title").text("扫码输入");
            $("#Runtime-ReceiveSamples-Modal").modal({
                backdrop: 'static',
                keyboard: false
            });
            LoadSampleID();
        });

        function LoadSampleID() {
            $('#Runtime-ReceiveSamples-upcTable').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
            var columns = [{ field: "sampleId" }];

            $('#Runtime-ReceiveSamples-upcTable').bootstrapTable({
                columns: columns,
                formatNoMatches: function () {  //没有匹配的结果
                    $('.fixed-table-toolbar').css("height", "0px"); /* 隐藏表格上的工具栏 */
                    return '扫码添加样品编号';
                },
                onLoadSuccess: function (data) {   // 数据加载成功后触发该事件
                    $('.fixed-table-toolbar').css("height", "0px"); /* 隐藏表格上的工具栏 */
                }
            });
        }

        function InputTable(obj) {
            var st = $(obj).val();  // 获取文本值
            $('#Runtime-ReceiveSamples-upcTable').bootstrapTable('prepend', [{ "sampleId": st }]);
            $("#Runtime-ReceiveSamples-confirm").attr("disabled", false);
            $(obj).val("");
        }

        $("#Runtime-ReceiveSamples-confirm").click(function () {
            var st = ""; // 获取表格中已所有样品编号
            $("#Runtime-ReceiveSamples-upcTable > tbody tr").each(function () {
                if ($(this).find("td:eq(0)")) {
                    st = st + "," + $(this).find("td:eq(0)").text();
                }
            });
            st = st.substring(1, st.lengths);
            $("#Runtime-ReceiveSamples-Modal").modal('hide');
            LoadSampling(st);
        });

    </script>
}
